// MenuBbsState.cpp: implementation of the CMenuBbsState class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MenuBbsState.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMenuBbsState::CMenuBbsState()
{
	m_oldItemIndex = -1;
}

CMenuBbsState::~CMenuBbsState()
{

}


void CMenuBbsState::Deactivate()
{
	clearOldRect();
}

BOOL CMenuBbsState::isMenuAccessChar(char c)
{
	return  ((c >= '/') && (c <= '9')) ||
		((c >= 'A') && (c <= '~'));
}

void CMenuBbsState::compactMenuItem(MenuItem *pItem, int right)
{
	char *line = m_pScreen->GetChars()[pItem->top];
	CComBSTR bs(right - pItem->left - 1, line + pItem->left + 2);
	/*
	int x;
	for(x = pItem->left + 2; x <= right; x++)
	{
		char c1 = line[x];
		char c2 = line[x + 1];
		if( ((c1 == '\xa8') && (c2 >= '\x40' && c2 <= '\x95')) ||
			((c1 == '\xa9') && (c2 >= '\x89' && c2 <= '\xEF')) ||
			((c1 == '\xa1') && (c2 >= '\xf4' && c2 <= '\xf6')) )
		{
			right = x - 1;
			break;
		}
	}
	for(x = right; x >= pItem->left + 2; x--)
	{
		if(line[x] != 32)
		{
			right = x;
			break;
		}
	}
	pItem->right = right;
	return;
	//*/
	int count = bs.Length();
	for(int n = 0; n < count; n++)
	{
		if(bs[n] >= 0x2500 && bs[n] <= 0x3013)
		{
			count = n;
			bs[count] = 0;
			break;
		}
	}
	for(n = count - 1; n >= 0; n--)
	{
		if(bs[n] != 32)
		{
			bs[n + 1] = 0;
			break;
		}
	}
	int w = n < 0? 0: CString(bs).GetLength();
	pItem->right = pItem->left + 1 + w;
}

void CMenuBbsState::clearOldRect()
{
	if(m_oldItemIndex != -1)
	{
		setScreenEffects(m_menuItems[m_oldItemIndex], EffectNone);
		m_oldItemIndex = -1;
	}
}

void CMenuBbsState::removeLessPatternItems()
{
	int n, count = m_menuItems.GetSize();
	char delimChars[4] = { ')', ']', '}', '.' };
	int delimCounts[4] = { 0, 0, 0, 0 };
	for(n = 0; n < count; n++)
	{
		for(int i = 0; i < 4; i++)
		{
			if(m_menuItems[n].delimChar == delimChars[i])
			{
				++delimCounts[i];
				break;
			}
		}
	}
	int maxDelimCount = delimCounts[0];
	int delimIndex = 0;
	for(n = 1; n < 4; n++)
	{
		if(delimCounts[n] > maxDelimCount)
		{
			maxDelimCount = delimCounts[n];
			delimIndex = n;
		}
	}
	char delimChar = delimChars[delimIndex];
	for(n = count - 1; n >= 0; n--)
	{
		char c = m_menuItems[n].delimChar; 
		if(c != delimChar)
		{
			m_menuItems.RemoveAt(n);
		}
	}
}